﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml;

namespace RibbonEditor.Controls.CommandUIDefinitions
{
    public partial class ButtonControl : UserControl, IControlRulable
    {
        private CommandUIDefinitionClasses.Button innerButton;

        internal CommandUIDefinitionClasses.Button InnerButton
        {
            get { return innerButton; }
            set { innerButton = value; }
        }

        public ButtonControl()
        {
            InitializeComponent();
            this.enumControlCommandType.Values = Enum.GetValues(typeof(Enumerations.CommandType));
            this.innerButton = new CommandUIDefinitionClasses.Button();
            this.FillControls();
        }

        public ButtonControl(CommandUIDefinitionClasses.Button buttonToDisplay): this()
        {
            this.innerButton = buttonToDisplay;
            this.FillControls();
        }

        private void FillControls()
        {
            try
            {
                if (this.innerButton != null)
                {
                    // Fill controls values
                    this.stringControlAlt.Value = this.innerButton.Alt;
                    this.referencedControlCommand.Value = this.InnerButton.Command;
                    this.enumControlCommandType.SelectedValue = this.innerButton.CommandType.ToString();
                    this.stringControlCommandValueId.Value = this.innerButton.CommandValueId;
                    this.stringControlDescription.Value = this.innerButton.Description;
                    this.stringControlId.Value = this.innerButton.Id;
                    this.referencedControlImage16by16.Value = this.innerButton.Image16by16;
                    this.referencedControlImage16by16Class.Value = this.innerButton.Image16by16Class;
                    this.integerControlImage16by16Left.Value = this.innerButton.Image16by16Left;
                    this.integerControlImage16by16Top.Value = this.innerButton.Image16by16Top;
                    this.referencedControlImage32by32.Value = this.innerButton.Image32by32;
                    this.referencedControlImage32by32Class.Value = this.innerButton.Image32by32Class;
                    this.integerControlImage32by32Left.Value = this.innerButton.Image32by32Left;
                    this.integerControlImage32by32Top.Value = this.innerButton.Image32by32Top;
                    this.stringControlLabelCss.Value = this.innerButton.LabelCss;
                    this.referencedControlLabelText.Value = this.innerButton.LabelText;
                    this.stringControlMenuItemId.Value = this.innerButton.MenuItemId;
                    this.integerControlSequence.Value = this.innerButton.Sequence;
                    this.stringControlTemplateAlias.Value = this.innerButton.TemplateAlias;
                    this.referencedControlToolTipDescription.Value = this.innerButton.ToolTipDescription;
                    this.stringControlToolTipHelpKeyWord.Value = this.innerButton.ToolTipHelpKeyWord;
                    this.referencedControlToolTipImage32by32.Value = this.innerButton.ToolTipImage32by32;
                    this.referencedControlToolTipImage32by32Class.Value = this.innerButton.ToolTipImage32by32Class;
                    this.integerControlToolTipImage32by32Left.Value = this.innerButton.ToolTipImage32by32Left;
                    this.integerControlToolTipImage32by32Top.Value = this.innerButton.ToolTipImage32by32Top;
                    this.stringControlToolTipShortcutKey.Value = this.innerButton.ToolTipShortcutKey;
                    this.referencedControlToolTipTitle.Value = this.innerButton.ToolTipTitle;
                }
            }
            catch (Exception error)
            {
                MessageBox.Show("An error occured: " + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void Save()
        {
            try
            {
                if (UserControlHelper.IsControlValid(this))
                {
                    this.innerButton.Alt = this.stringControlAlt.Value;
                    this.InnerButton.Command = this.referencedControlCommand.Value;
                    this.innerButton.CommandType = (Enumerations.CommandType)Enum.Parse(typeof(Enumerations.CommandType), this.enumControlCommandType.SelectedValue);
                    this.innerButton.CommandValueId = this.stringControlCommandValueId.Value;
                    this.innerButton.Description = this.stringControlDescription.Value;
                    this.innerButton.Id = this.stringControlId.Value;
                    this.innerButton.Image16by16 = this.referencedControlImage16by16.Value;
                    this.innerButton.Image16by16Class = this.referencedControlImage16by16Class.Value;
                    this.innerButton.Image16by16Left = this.integerControlImage16by16Left.Value;
                    this.innerButton.Image16by16Top = this.integerControlImage16by16Top.Value;
                    this.innerButton.Image32by32 = this.referencedControlImage32by32.Value;
                    this.innerButton.Image32by32Class = this.referencedControlImage32by32Class.Value;
                    this.innerButton.Image32by32Left = this.integerControlImage32by32Left.Value;
                    this.innerButton.Image32by32Top = this.integerControlImage32by32Top.Value;
                    this.innerButton.LabelCss = this.stringControlLabelCss.Value;
                    this.innerButton.LabelText = this.referencedControlLabelText.Value;
                    this.innerButton.MenuItemId = this.stringControlMenuItemId.Value;
                    this.innerButton.Sequence = this.integerControlSequence.Value;
                    this.innerButton.TemplateAlias = this.stringControlTemplateAlias.Value;
                    this.innerButton.ToolTipDescription = this.referencedControlToolTipDescription.Value;
                    this.innerButton.ToolTipHelpKeyWord = this.stringControlToolTipHelpKeyWord.Value;
                    this.innerButton.ToolTipImage32by32 = this.referencedControlToolTipImage32by32.Value;
                    this.innerButton.ToolTipImage32by32Class = this.referencedControlToolTipImage32by32Class.Value;
                    this.innerButton.ToolTipImage32by32Left = this.integerControlToolTipImage32by32Left.Value;
                    this.innerButton.ToolTipImage32by32Top = this.integerControlToolTipImage32by32Top.Value;
                    this.innerButton.ToolTipShortcutKey = this.stringControlToolTipShortcutKey.Value;
                    this.innerButton.ToolTipTitle = this.referencedControlToolTipTitle.Value;

                    UserControlHelper.CleanDirtyFields(this);
                }
                else
                {
                    MessageBox.Show(this, "Please fill all required fields", "Warning", MessageBoxButtons.OK, MessageBoxIcon.Warning);
                }
            }
            catch (Exception error)
            {
                MessageBox.Show(this, "An error occured:" + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        private void ButtonControl_Leave(object sender, EventArgs e)
        {
            try
            {
                if (UserControlHelper.LeaveControl(this))
                {
                    this.Save();
                }
            }
            catch (Exception error)
            {
                MessageBox.Show(this, "An error occured:" + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }

        public void BuildObjectXml(XmlNode parentNode)
        {
            try
            {
                this.innerButton.AddNode(parentNode);
            }
            catch (Exception error)
            {
                MessageBox.Show(this, "An error occured:" + error.ToString(), "Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
        }
    }
}
